#include "checkinmanagementpage.h"
#include "ui_checkinmanagementpage.h"
#include "sqlhelper.h"
#include <QMessageBox>
#include "dialogs/modifycheckindialog.h"

CheckinManagementPage::CheckinManagementPage(QWidget *parent)
    : MyWidget(parent)
    , ui(new Ui::CheckinManagementPage)
{
    ui->setupUi(this);
    loadStudentData();
    connect(ui->tableWidgetCheckin, &QTableWidget::itemSelectionChanged, this, &CheckinManagementPage::on_tableWidgetCheckin_itemSelectionChanged);
}


CheckinManagementPage::~CheckinManagementPage()
{
    delete ui;
}

void CheckinManagementPage::loadStudentData()
{
    QList<QVariantList> checkins = SqlHelper::getInstance().queryCheckIn();

    ui->tableWidgetCheckin->clearContents();
    ui->tableWidgetCheckin->setRowCount(0);

    ui->tableWidgetCheckin->setColumnCount(8);
    ui->tableWidgetCheckin->setHorizontalHeaderLabels(QStringList()
            << "编号" << "姓名" << "学号" << "打卡日期" << "打卡时间" << "打卡状态" << "备注" << "创建日期");

    ui->tableWidgetCheckin->horizontalHeader()->setStretchLastSection(true);

    for (const QVariantList &checkin : checkins)
    {
        int row = ui->tableWidgetCheckin->rowCount();
        ui->tableWidgetCheckin->insertRow(row);

        ui->tableWidgetCheckin->setItem(row, 0, new QTableWidgetItem(checkin[0].toString()));
        ui->tableWidgetCheckin->setItem(row, 1, new QTableWidgetItem(checkin[1].toString()));
        ui->tableWidgetCheckin->setItem(row, 2, new QTableWidgetItem(checkin[2].toString()));
        ui->tableWidgetCheckin->setItem(row, 3, new QTableWidgetItem(checkin[3].toString()));
        ui->tableWidgetCheckin->setItem(row, 4, new QTableWidgetItem(checkin[4].toString()));
        ui->tableWidgetCheckin->setItem(row, 5, new QTableWidgetItem(checkin[5].toString()));
        ui->tableWidgetCheckin->setItem(row, 6, new QTableWidgetItem(checkin[6].toString()));
        ui->tableWidgetCheckin->setItem(row, 7, new QTableWidgetItem(checkin[7].toString()));
    }
}

void CheckinManagementPage::on_tableWidgetCheckin_itemSelectionChanged()
{
    selectedRow = ui->tableWidgetCheckin->currentRow();
}

void CheckinManagementPage::on_pushButtonModify_clicked()
{
    if (selectedRow == -1) {
        QMessageBox::warning(this, "警告", "请选中一行用户数据！");
        return;
    }

    // 获取选中行的数据
    int id = ui->tableWidgetCheckin->item(selectedRow, 0)->text().toInt();
    QString studentName = ui->tableWidgetCheckin->item(selectedRow, 1)->text();
    QString studentId = ui->tableWidgetCheckin->item(selectedRow, 2)->text();
    QString checkinDate = ui->tableWidgetCheckin->item(selectedRow, 3)->text();
    QString checkinTime = ui->tableWidgetCheckin->item(selectedRow, 4)->text();
    QString status = ui->tableWidgetCheckin->item(selectedRow, 5)->text();
    QString remarks = ui->tableWidgetCheckin->item(selectedRow, 6)->text();


    // 创建修改用户对话框并传递数据
    ModifyCheckinDialog dialog(this, studentName, studentId, checkinDate, checkinTime, status, remarks);

    if (dialog.exec() == QDialog::Accepted) {
        // 获取用户输入的修改数据
        QString studentName = dialog.getStudentName();
        QString studentId = dialog.getStudentId();
        QString checkinDate = dialog.getCheckinDate();
        QString checkinTime = dialog.getCheckinTime();
        QString status = dialog.getStatus();
        QString remarks = dialog.getRemarks();

        qDebug() << studentName;

        bool success = SqlHelper::getInstance().updateCheckin(id, studentName, studentId, checkinDate, checkinTime, status, remarks);

        if (success) {
            QMessageBox::information(this, "成功", "用户信息已更新！");
            loadStudentData();
        } else {
            QMessageBox::critical(this, "错误", "更新用户信息失败！");
        }
    }
}

void CheckinManagementPage::on_pushButtonDelete_clicked()
{
    if (selectedRow == -1) {
        QMessageBox::warning(this, "警告", "请选中一行用户数据！");
        return;
    }

    QString id = ui->tableWidgetCheckin->item(selectedRow, 0)->text();

    QMessageBox::StandardButton reply = QMessageBox::question(this, "确认删除", "确定要删除编号为 " + id + " 的记录吗？",
                                                                QMessageBox::Yes | QMessageBox::No);

    if (reply == QMessageBox::Yes) {

        if (SqlHelper::getInstance().deleteCheckin(id.toInt())) {
            QMessageBox::information(this, "成功", "编号为 " + id + " 的记录已删除！");
            loadStudentData();
        }
        else {
            QMessageBox::critical(this, "错误", "删除编号为 " + id + " 的记录失败！");
        }
    }
}

